package com.cn.web.controller.PartsManagement;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.cn.bean.pojo.PartsManagement.BusinessTypeTable;
import com.cn.bean.vo.PartsManagement.TeamPersonnel;
import com.cn.bean.vo.bc.UserJob;
import com.cn.config.config.Result;
import com.cn.service.server.PartsManagement.BusinessTypeService;
import com.cn.service.server.PartsManagement.TeamPersonnelService;
import com.cn.service.server.admin.UserDepartmentsServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/Team")
@Transactional
public class TeamPersonnelController {
    private final TeamPersonnelService teamPersonnelService;

    @Autowired(required = false)
    public TeamPersonnelController(TeamPersonnelService teamPersonnelService) {
        this.teamPersonnelService = teamPersonnelService;
    }

    @RequestMapping("/select/TeamPersonnel")
    @ResponseBody
    public List<TeamPersonnel> selectTeamPersonnel(@RequestBody JSONObject jsonObject) {
        Integer page = jsonObject.getInteger("page");
        Integer limit = jsonObject.getInteger("limit");
        String username = jsonObject.getString("inputUsername");
        String name = jsonObject.getString("inputName");
        String jobName = jsonObject.getString("inputJobName");
        String teamName = jsonObject.getString("inputTeamName");
        username = (username != null) ? username : "";
        name = (name != null) ? name : "";
        jobName = (jobName != null) ? jobName : "";
        teamName = (teamName != null) ? teamName : "";
        return teamPersonnelService.selectTeamPersonnel(limit, page, username, name, jobName, teamName);
    }

    @RequestMapping("/select/Team/Auto1")
    @ResponseBody
    public List<TeamPersonnel> selectTeamAuto1(@RequestBody JSONObject jsonObject) {
        Integer page = jsonObject.getInteger("page");
        Integer limit = jsonObject.getInteger("limit");
        String name = jsonObject.getString("inputName");
        String code = jsonObject.getString("inputCode");
        code = (code != null) ? code : "";
        name = (name != null) ? name : "";
        return teamPersonnelService.selectTeamAuto1(limit, page, code, name);
    }

    @RequestMapping("/select/Team/Auto1/tc")
    @ResponseBody
    public List<TeamPersonnel> selectTeamAuto1Tc(@RequestBody JSONObject jsonObject) {
        Integer page = jsonObject.getInteger("page");
        Integer limit = jsonObject.getInteger("limit");
        String name = jsonObject.getString("input1");
        String code = jsonObject.getString("input2");
        code = (code != null) ? code : "";
        name = (name != null) ? name : "";
        return teamPersonnelService.selectTeamAuto1(limit, page, code, name);
    }

    @RequestMapping("/select/Team")
    @ResponseBody
    public List<TeamPersonnel> selectTeam(@RequestBody JSONObject jsonObject) {
        Integer page = jsonObject.getInteger("page");
        Integer limit = jsonObject.getInteger("limit");
        String name = jsonObject.getString("inputName");
        String code = jsonObject.getString("inputCode");
        code = (code != null) ? code : "";
        name = (name != null) ? name : "";
        return teamPersonnelService.selectTeam(limit, page, code, name);
    }

    @RequestMapping("/select/Employee/Job")
    @ResponseBody
    public List<UserJob> selectEmployeeJob(@RequestBody JSONObject jsonObject) {
        Integer page = jsonObject.getInteger("page");
        Integer limit = jsonObject.getInteger("limit");
        String jobName = jsonObject.getString("input1");
        String name = jsonObject.getString("input2");
        String username = jsonObject.getString("input3");

        jobName = (jobName != null) ? jobName : "";
        name = (name != null) ? name : "";
        username = (username != null) ? username : "";
        return teamPersonnelService.selectEmployeeJob(limit, page, jobName, name, username);
    }

    @RequestMapping("/select/User/Information")
    @ResponseBody
    public List<UserJob> selectUserInformation(@RequestBody JSONObject jsonObject) {
        Integer page = jsonObject.getInteger("page");
        Integer limit = jsonObject.getInteger("limit");
        String name = jsonObject.getString("input1");
        String username = jsonObject.getString("input2");
        name = (name != null) ? name : "";
        username = (username != null) ? username : "";
        List<UserJob> userJobs = teamPersonnelService.selectUserInformation(limit,page , name, username);
        return userJobs;
    }

    @RequestMapping("/select/Job/Information")
    @ResponseBody
    public List<UserJob> selectJobInformation(@RequestBody JSONObject jsonObject) {
        Integer page = jsonObject.getInteger("page");
        Integer limit = jsonObject.getInteger("limit");
        String jobName = jsonObject.getString("input1");
        jobName = (jobName != null) ? jobName : "";

        return teamPersonnelService.selectJobInformation(limit, page, jobName);
    }

    @RequestMapping("/add/Team")
    @ResponseBody
    public Result addTeam(@RequestBody JSONObject jsonObject) {
        String creationTime = jsonObject.getString("creationTime");
        creationTime = creationTime.replace("/", "-");
        String TeamName = jsonObject.getString("TeamName");
        String description = jsonObject.getString("description");
        Integer status = jsonObject.getInteger("status");
        JSONArray childList = jsonObject.getJSONArray("childList");
        if (childList.size() == 0) {
            return Result.failure("500", "请填写班组的人员");

        }
        List<TeamPersonnel> teamPersonnelList = new ArrayList<>();
        for (int i = 0; i < childList.size(); i++) {
            JSONObject childObject = childList.getJSONObject(i);
            // 从 childObject 中获取 TeamPersonnel 对象的属性
            int personnelID = childObject.getInteger("personnelID");
            int jobId = childObject.getInteger("jobID");
            int TPMStatus = childObject.getInteger("status");
            TeamPersonnel personnel = new TeamPersonnel();
            personnel.setUserId(personnelID);
            personnel.setJobId(jobId);
            personnel.setTPMStatus(TPMStatus);
            teamPersonnelList.add(personnel);
        }
        Result result = teamPersonnelService.addTeam(TeamName, description, status, creationTime, teamPersonnelList);
        return result;
    }

    @RequestMapping("/delete/Team/EmployeeJob")
    @ResponseBody
    public Result deleteTeamEmployeeJob(@RequestBody JSONObject jsonObject) {

        JSONArray deleteIdList = jsonObject.getJSONArray("deleteIdList");
        List<TeamPersonnel> idList = new ArrayList<>();
        for (int i = 0; i < deleteIdList.size(); i++) {
            JSONObject deleteIdObject = deleteIdList.getJSONObject(i);
            int id = deleteIdObject.getInteger("teamID");
            TeamPersonnel personnel = new TeamPersonnel();
            personnel.setTeamID(id);
            idList.add(personnel);
        }
        return teamPersonnelService.deleteTeamEmployeeJob(idList);
    }

    @RequestMapping("/select/TeamPersonnel/TeamId")
    @ResponseBody
    public List<TeamPersonnel> selectTeamPersonnelTeamId(@RequestBody JSONObject jsonObject) {
        Integer TeamId = jsonObject.getInteger("TeamId");
        return teamPersonnelService.selectTeamPersonnelTeamId(TeamId);
    }

    @RequestMapping("/upDate/Team")
    @ResponseBody
    public Result upDateTeam(@RequestBody JSONObject jsonObject) {
        Integer TeamID = jsonObject.getInteger("TeamID");
        String TeamName = jsonObject.getString("TeamName");
        String description = jsonObject.getString("description");
        Integer status = jsonObject.getInteger("status");
        JSONArray childList = jsonObject.getJSONArray("childList");
        if (childList.size() == 0) {
            Result.failure("500", "请填写班组的人员");
        }
        List<TeamPersonnel> teamPersonnelList = new ArrayList<>();
        for (int i = 0; i < childList.size(); i++) {
            JSONObject childObject = childList.getJSONObject(i);
            // 从 childObject 中获取 TeamPersonnel 对象的属性
            int personnelID = childObject.getInteger("personnelID");
            int jobId = childObject.getInteger("jobID");
            int TPMStatus = childObject.getInteger("status");
            TeamPersonnel personnel = new TeamPersonnel();
            personnel.setUserId(personnelID);
            personnel.setJobId(jobId);
            personnel.setTPMStatus(TPMStatus);
            teamPersonnelList.add(personnel);
        }
        Result result = teamPersonnelService.upDateTeam(TeamID, TeamName, description, status, teamPersonnelList);
        return result;
    }

}
